package com.write.program.gui;

import java.awt.*;
import java.awt.event.*;

public class ColorGui extends Frame implements AdjustmentListener, ItemListener, ActionListener {
    Button[] buttonArray = new Button[6];
    String[] namesOfColorsArray = new String[]{"Черный", "Синий", "Желтый", "Зеленый", "Красный", "Голубой"};
    Color[] colorArray = new Color[]{Color.black, Color.blue, Color.yellow, Color.green, Color.red, Color.cyan};
    Scrollbar scrollbar;
    List list;
    Canvas canvas;
    Color bgColor;
    Color circlesColor = this.colorArray[0];

    int circlesRadius = 50;
    int scrollbarValue;

    public ColorGui() {
        setLayout(new GridLayout(2, 1));

        Panel panel1 = new Panel();
        panel1.setLayout(new BorderLayout());
        this.canvas = new Canvas() {
            public void paint(Graphics param1Graphics) {
                int i = (ColorGui.this.canvas.getSize()).height;
                int j = (ColorGui.this.canvas.getSize()).width;
                int k = (j / 2 - ColorGui.this.circlesRadius) * ColorGui.this.scrollbarValue / ColorGui.this.scrollbar.getMaximum();
                int m = j - ColorGui.this.circlesRadius - (j / 2 - ColorGui.this.circlesRadius) * ColorGui.this.scrollbarValue / ColorGui.this.scrollbar.getMaximum();
                param1Graphics.fillOval(j / 2, i / 2, 5, 5);

                param1Graphics.setColor(ColorGui.this.circlesColor);
                param1Graphics.fillOval(k, (i - ColorGui.this.circlesRadius) / 2, ColorGui.this.circlesRadius, ColorGui.this.circlesRadius);
                param1Graphics.fillOval(m, (i - ColorGui.this.circlesRadius) / 2, ColorGui.this.circlesRadius, ColorGui.this.circlesRadius);
            }
        };
        panel1.add(this.canvas);
        this.scrollbar = new Scrollbar(0, 0, 5, 0, 100);
        this.scrollbar.addAdjustmentListener(this);
        panel1.add(this.scrollbar, "South");
        add(panel1);


        Panel panel2 = new Panel();

        panel2.setBackground(Color.pink);
        panel2.setLayout(new GridLayout(1, 2));

        Panel panel3 = new Panel();
        panel3.setLayout(new BorderLayout());
        Label label1 = new Label("Выбор цвета кругов");
        label1.setAlignment(1);
        panel3.add(label1, "North");
        this.list = new List(4, false);
        this.list.addItemListener(this);
        for (byte b1 = 0; b1 < this.namesOfColorsArray.length; b1++) {
            this.list.add(this.namesOfColorsArray[b1]);
        }
        this.list.select(0);
        panel3.add(this.list, "Center");
        panel2.add(panel3);

        Panel panel4 = new Panel();
        panel4.setLayout(new BorderLayout());
        Panel panel5 = new Panel();
        Label label2 = new Label("Выбор цвета фона");
        label2.setAlignment(1);
        panel4.add(label2, "North");
        for (byte b2 = 0; b2 < this.buttonArray.length; b2++) {
            this.buttonArray[b2] = new Button(this.namesOfColorsArray[b2]);
            this.buttonArray[b2].setBackground(this.colorArray[b2]);
            this.buttonArray[b2].setForeground(Color.white);
            this.buttonArray[b2].addActionListener(this);
            panel5.add(this.buttonArray[b2]);
        }
        panel4.add(panel5, "Center");
        panel2.add(panel4);
        add(panel2);
    }


    public void actionPerformed(ActionEvent paramActionEvent) {
        Button button = (Button) paramActionEvent.getSource();
        this.bgColor = button.getBackground();
        this.canvas.setBackground(this.bgColor);
    }


    public void adjustmentValueChanged(AdjustmentEvent paramAdjustmentEvent) {
        if (paramAdjustmentEvent.getValue() == this.scrollbar.getMaximum() - this.scrollbar.getVisibleAmount()) {
            this.scrollbarValue = this.scrollbar.getMaximum();
        } else {
            this.scrollbarValue = paramAdjustmentEvent.getValue();
        }
        this.canvas.repaint();
    }

    public void itemStateChanged(ItemEvent paramItemEvent) {
        int i = this.list.getSelectedIndex();
        this.circlesColor = this.colorArray[i];
        this.canvas.repaint();
    }

    public static void main(String[] paramArrayOfString) {
        ColorGui colorGui = new ColorGui();
        colorGui.setSize(400, 400);
        colorGui.setLocation(100, 100);
        colorGui.setVisible(true);
        colorGui.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent param1WindowEvent) {
                System.exit(0);
            }
        });
    }
}


